// 创建学校相关的小仓库
import { defineStore } from "pinia";
import type { SchoolState } from "./types/types";
import type { schoolAllResponseData, schoolInfoData, schoolListFormData, schoolListResponseData, schoolResponseData } from "@/api/school/type";
import { reqSchoolAdd, reqSchoolAll, reqSchoolById, reqSchoolDelete, reqSchoolList, reqSchoolUpdata } from "@/api/school";


let useSchoolStore = defineStore("School", {
    state: (): SchoolState => {
        return {
            schools: []
        }
    },
    actions: {
        // 获取所有学校
        async getSchoolAll() {

            let result: schoolAllResponseData = await reqSchoolAll()
            if (result.status == 0) {
                this.schools = (result.data as schoolInfoData[])

                return 'ok'
            } else {
                return Promise.reject(new Error(result.msg))
            }
        },
        // 分页获取所有学校
        async getSchoolList(data: schoolListFormData) {

            let result: schoolListResponseData = await reqSchoolList(data);
            if (result.status == 0) {
                this.schools = (result.data?.data as schoolInfoData[])

                return result.data
            } else {
                return Promise.reject(new Error(result.msg))
            }
        },
        // 添加学校
        async addSchool(data: schoolInfoData) {
            let result: schoolResponseData = await reqSchoolAdd(data);
            if (result.status == 0) {
                return "ok"
            } else {
                return Promise.reject(new Error(result.msg))
            }
        },
        // 查找所有学校信息（通过id）
        async getSchoolById(_id: string) {

            let result: schoolResponseData = await reqSchoolById(_id);
            if (result.status == 0) {
                return result.data
            } else {
                return Promise.reject(new Error(result.msg))
            }
        },
        //  修改学校信息
        async upDataSchool(data:schoolInfoData) {

            let result: schoolResponseData = await reqSchoolUpdata(data);
            if (result.status == 0) {
                return "ok"
            } else {
                return Promise.reject(new Error(result.msg))
            }
        },
        // 删除学校
        async deleteSchool(schoolId: string) {

            let result: schoolResponseData = await reqSchoolDelete(schoolId);
            if (result.status == 0) {
                return "ok"
            } else {
                return Promise.reject(new Error(result.msg))
            }
        },
    }
})
export default useSchoolStore